package com.herench.bs.handler;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.dao.IClinicAppointmentRecordDao;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.utils.DataFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * cleverClinic
 * com.herench.bs.handler
 *
 * @author wangwenkai
 * @date 2017/11/8/13:10
 */
@Component
@Handler(code = "102105" ,name="保存待缴费数据")
public class SaveclinicChargeDataHandler extends AbstractMessageHandler {

    @Autowired
    private IClinicAppointmentRecordDao clinicAppointmentRecordDao;

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        Assert.notNull(channelCode, "channelCode 不可为空！");
        String recordId = requestMessage.getArgsPropertyStringValue("recordId");
        Assert.notNull(recordId, "recordId 不可为空！");
        String patientId = requestMessage.getArgsPropertyStringValue("patientId");
        Assert.notNull(patientId, "patientId 不可为空！");
        String paySeq = requestMessage.getArgsPropertyStringValue("paySeq");
        Assert.notNull(paySeq, "paySeq 不可为空！");
//        String payType = requestMessage.getArgsPropertyStringValue("payType");
//        Assert.notNull(payType, "payType 不可为空！");
        String cardType = requestMessage.getArgsPropertyStringValue("cardType");
        String cardNumber = requestMessage.getArgsPropertyStringValue("cardNumber");
        String userName = requestMessage.getArgsPropertyStringValue("userName");
        Assert.notNull(userName, "userName 不可为空！");
        String source = requestMessage.getArgsPropertyStringValue("source");
//        Assert.notNull(source, "source 不可为空！");
        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        Map<String, Object> params = new HashMap<>(16);
        params.put("channelCode", channelCode);
        params.put("id", recordId);
        params.put("hospitalCode", hospitalCode);
        Map<String, Object> resDao = clinicAppointmentRecordDao.findOne(params);
        String idCard = (String) resDao.get("idCard");
        String resDate = DataFormatUtils.getSimpleTimeString((Date) resDao.get("appointDate"));
        String transactNo = (String) resDao.get("tranNo");
        String visitNo = (String) resDao.get("visitNo");
        Map<String, Object> res = new HashMap<>(16);

        Map<String, Object> resUnpaidOrders = outpatientClinicAgent.loadUnpaidOrders(channelCode, hospitalCode, idCard, source, userName, "3");
        if (Dialect.getSuccessfulState().equals(resUnpaidOrders.get("code"))) {
            res.put("dataList",resUnpaidOrders.get("dataList"));
            res.put(ResponseMessage.RES_CODE, "-001");
            res.put(ResponseMessage.MESSAGE, "存在未支付订单");
            res.put(ResponseMessage.MESSAGE_IN, "存在未支付订单");
            res.put(ResponseMessage.MESSAGE_OUT, "存在未支付订单");
        } else {
            res = outpatientClinicAgent.saveclinicChargeData(channelCode, hospitalCode, userName, recordId, idCard, resDate, transactNo, patientId, visitNo, paySeq, cardType, cardNumber);
        }
        responseMessage.setData(res);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
